from django import forms
from app01 import models
from django.core.validators import RegexValidator, EmailValidator, ValidationError
from django_redis import get_redis_connection



class RegisterForm(forms.ModelForm):
    mobile_phone = forms.CharField(label='手机号', required=True,min_length=11,max_length=11,validators=[RegexValidator(r'^(1[3|4|5|6|7|8|9])\d{9}$', '手机号格式错误'), ],
                                   error_messages={'required':'手机号不能为空'})
    password = forms.CharField(label='密码',min_length=8,required=True,widget=forms.PasswordInput,
                               error_messages={'required':'密码不能为空','min_length':'密码不能少于8位'})
    confirm_password = forms.CharField(label='重复密码',required=True,widget=forms.PasswordInput,
                                       error_messages={'required':'密码不能为空','min_length':'密码不能少于8位'})
    code = forms.CharField(label='验证码',widget=forms.TextInput())
    username = forms.CharField(label='用户名',min_length=6,required=True,validators=[EmailValidator('用户名格式错误')],
                               error_messages={'required':'用户名不能为空','min_length':'密码不能少于6位'})
    class Meta:
        model = models.UserInfo
        fields = ['username', 'email', 'password', 'confirm_password', 'mobile_phone', 'code']

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for name, field in self.fields.items():
            field.widget.attrs['class'] = 'form-control'
            field.widget.attrs['placeholder'] = '请输入%s' % (field.label,)